package ink.xiaobaibai.service;

import ink.xiaobaibai.common.delay.DeviceDelayItem;
import ink.xiaobaibai.service.monthly.impl.DelayTaskCommonServiceImpl;
import ink.xiaobaibai.service.monthly.impl.DelayTimedTaskServiceImpl;
import ink.xiaobaibai.serviceOxy.monthly.impl.DelayMonControlServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.stereotype.Service;

import javax.annotation.PreDestroy;

/**
 * @description: 关机保护服务
 * @author: 小白白
 * @create: 2021-06-11
 **/

@Service
public class ShutdownProtectionService implements ApplicationRunner {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DelayMonControlServiceImpl delayMonDeviceControlService;

    @Autowired
    private DelayTaskCommonServiceImpl delayTaskCommonService;

    @Autowired
    private DelayTimedTaskServiceImpl delayTimedTaskService;

    private static final String clientDelayTimedSafe = "clientDelayTimedSafe";

    private static final String clientMonthlySafe = "clientMonthlySafe";

    private static final String clientCouponSafe = "clientCouponSafe";

    /**
     * 客户端:
     * 1.单次延时
     * 2.月费延时
     * 3.优惠券延时
     * 4.定时延时(忽略定时延时队列,因为每天0点刷新,可以忍受)
     */

    @PreDestroy
    public void destroy() throws Exception {
        this.redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                for (DeviceDelayItem item : ShutdownProtectionService.this.delayTimedTaskService.delayQueue.set) {
                    redisOperations.opsForSet().add(clientDelayTimedSafe, item);
                }
                for (DeviceDelayItem item : ShutdownProtectionService.this.delayMonDeviceControlService.delayQueue.set) {
                    redisOperations.opsForSet().add(clientMonthlySafe, item);
                }
                for (DeviceDelayItem item : ShutdownProtectionService.this.delayTaskCommonService.delayQueue.set) {
                    redisOperations.opsForSet().add(clientCouponSafe, item);
                }
                return null;
            }
        });
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        /**
         * 还原
         */
        this.delayTimedTaskService.delayQueue.addAll(this.redisTemplate.opsForSet().members(clientDelayTimedSafe));
        this.redisTemplate.delete(clientDelayTimedSafe);

        this.delayMonDeviceControlService.delayQueue.addAll(this.redisTemplate.opsForSet().members(clientMonthlySafe));
        this.redisTemplate.delete(clientMonthlySafe);

        this.delayTaskCommonService.delayQueue.addAll(this.redisTemplate.opsForSet().members(clientCouponSafe));
        this.redisTemplate.delete(clientCouponSafe);
    }

}
